const formatTime = dateStr => {
  const date = new Date(dateStr.replace('T', ' ').replace(/-/g, '/'));
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const createDotList = function(total,active){
  let targetList = [];
    for (let i = 0; i < total; i++) {
      if (i < active) {
        targetList.push('active');
      } else if (i === active) {
        targetList.push('ongoing');
      } else {
        targetList.push('');
      }
    }
    return targetList;
  }

/**
 * 监听输入框变化
 */
const inputEidt = function(e) {
  let name = e.currentTarget.dataset.name;
  let nameMap = {}
  if (name.indexOf('.') > 0) {
    let nameList = name.split('.')
    if (this.data[nameList[0]]) {
      nameMap[nameList[0]] = this.data[nameList[0]]
    } else {
      nameMap[nameList[0]] = {}
    }
    nameMap[nameList[0]][nameList[1]] = e.detail.value
  } else {
    nameMap[name] = e.detail.value
  }
  this.setData(nameMap);
}

// 提示错误信息  
const isError = function(msg, that) {
  that.setData({
    showTopTips: true,
    errorMsg: msg
  })
}

// 清空错误信息  
const clearError = function(that) {
  that.setData({
    showTopTips: false,
    errorMsg: ""
  })
}


module.exports = {
  formatTime: formatTime,
  inputEidt: inputEidt,
  createDotList: createDotList,
  isError: isError,
  clearError: clearError
}
